// import Login from"@/components/Login"


// export default function Page() {
//     return <Login />  
//     // 返回组件
// }
"use client"
import React,{ useState } from "react";

function Login() {
    const [username, setUsername] = useState("");
    const [password, setPassword] = useState("");

    const handleSubmit = async(e) => {
        e.preventDefault();
        // 处理登录逻辑
        console.log("表单数据:"+username,password);

        const response = await fetch("http://127.0.0.1:8000/api/login/login", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({ username, password }),
        })

        const result = await response.json();
        console.log("result"+result);

        if(response.ok){
            
            console.log("token",result.token);
//保存token到localStorage或cookies中
// document.cookie = `token=${result.token}; SameSite=None; Secure`;
document.cookie = `token=${result.token}; path=/;`;
    console.log("登录成功");
    //跳转
    // window.location.href = "/coures";
        }else{
            console.log("登录失败,请检查用户名和密码");
        }
        console.log("client");
    }

    return (
        <div>
            <h1>登入页面</h1>
            <form onSubmit={handleSubmit}>
                <label htmlFor="email">邮箱:</label>
                <input id="username"value={username}onChange={(e) => setUsername(e.target.value)}/>
                <br/>
                <label htmlFor="password">密码:</label>
                <input type="password" id="password"value={password}onChange={(e) => setPassword(e.target.value)}/>
                <br/>
                <button type="submit">登入</button>
            </form>
            </div>
            )
}
export default Login;